package com.sun.services.roles.impls;


import com.sun.commons.base.BaseService;
import com.sun.commons.base.R;
import com.sun.dto.users.RoleDto;
import com.sun.dto.users.RoleSearchModel;
import com.sun.dto.users.UserRoleDto;
import com.sun.entitys.users.UserRole;
import com.sun.services.roles.RoleMapper;
import com.sun.services.roles.service.UserRoleService;
import com.sun.services.users.envent.UserAddEvent;
import com.sun.services.users.repository.UserRoleRepository;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.modulith.events.ApplicationModuleListener;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * MP自动填充Handler
 *
 * @Author whsunchunbo@qq.com
 * @CreateTime 2025/9/9 06:13
 */
@Service
@AllArgsConstructor
public class UserRoleServiceImpl extends BaseService implements UserRoleService {

    private final UserRoleRepository userRoleRepository;

    private final RoleMapper mapper;


    @Override
    public void deleteByUserId(Long userId) {

    }

    @ApplicationModuleListener
    public void onUserAddEvent(UserAddEvent event) {
        event.getRoleIds().forEach(roleId -> {
            saveUserRole(new UserRoleDto(nextId(), event.getUserId(), roleId));
        });

    }

    @Override
    public void saveUserRole(UserRoleDto userRole) {
        UserRole entity = mapper.toUserRoleEntity(userRole);
        userRoleRepository.save(entity);
    }

    @Override
    public R findByUserId(Long userId) {


        return null;
    }

    @Override
    public List<UserRoleDto> getList(RoleSearchModel model) {
        return List.of();
    }

    @Override
    public Page<UserRoleDto> getPage(RoleSearchModel model) {
        return null;
    }

}
